<?php
class hrd_pph21_model extends MY_Model {
	var $nama_tabel = 'hrd_pph21';

	function GetJSON($idPeriode) {
		$ls_sql	= "	SELECT	a.*, b.nama_karyawan, c.nama_periode, d.ket_status, b.alamat_jalan, b.npwp, b.no_ktp, b.id_karyawan_pk
					FROM	".$this->nama_tabel." a
					LEFT JOIN hrd_gaji g ON a.id_gaji_fk = g.id_gaji_pk
					LEFT JOIN hrd_karyawan b ON g.id_karyawan_fk=b.id_karyawan_pk
					LEFT JOIN hrd_periode c ON g.id_periode_fk=c.id_periode_pk
					LEFT JOIN hrd_status_kawin d ON b.id_status_kawin_fk = d.id_status_kawin_pk
					WHERE g.id_periode_fk = '".$idPeriode."'
					ORDER BY ".(post('sort')=='' ? 'b.nama_karyawan' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		$list = $this->db->query($ls_sql)->result_array();

		// Ambil Jumlah
		$sql = "select sum(gaji_pokok) as gaji_pokok, sum(tunjangan) as tunjangan, sum(tunjangan_pajak) as tunjangan_pajak, sum(penghasilan_bruto) as penghasilan_bruto, sum(biaya_jabatan) as biaya_jabatan, sum(jht) as jht, sum(total_pengurang) as total_pengurang, sum(penghasilan_neto) as penghasilan_neto, sum(ptkp) as ptkp, sum(penghasilan_kena_pajak) as penghasilan_kena_pajak, sum(pajak) as pajak
				from ".$this->nama_tabel." a
				join hrd_gaji b ON a.id_gaji_fk = b.id_gaji_pk
				where b.id_periode_fk = '".$idPeriode."'
				";
		$sum = $this->db->query($sql)->row_array();
		$footer[] = array('nama_periode' => '<b>Total</b>',
						  'gaji_pokok' => $sum['gaji_pokok'],
						  'tunjangan' => $sum['tunjangan'],
						  'tunjangan_pajak' => $sum['tunjangan_pajak'],
						  'penghasilan_bruto' => $sum['penghasilan_bruto'],
						  'biaya_jabatan' => $sum['biaya_jabatan'],
						  'jht' => $sum['jht'],
						  'total_pengurang' => $sum['total_pengurang'],
						  'penghasilan_neto' => $sum['penghasilan_neto'],
						  'ptkp' => $sum['ptkp'],
						  'penghasilan_kena_pajak' => $sum['penghasilan_kena_pajak'],
						  'pajak' => $sum['pajak']
						  );
		$data = array('total' => count($list), 'rows' => $list, 'footer' => $footer);

		return json_encode( $data );
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_g13_pk' => $arr_rule['id_g13_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	// Untuk detail G13
	/*function GetDetailJSON($idG13) {
		$ls_sql	= "	SELECT	a.*, b.nama_kode_bayar, b.id_grub_kode_bayar_fk
					FROM	hrd_detail_g13 a
					LEFT JOIN hrd_kode_bayar b ON a.id_kode_bayar_fk = b.id_kode_bayar_pk
					where id_g13_fk = '".$idG13."'
					ORDER BY ".(post('sort')=='' ? 'id_g13_fk' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		$list = $this->db->query($ls_sql)->result_array();
		// Memisahkan pendapatan dan potongan
		$jmlPendapatan=0;
		$jmlPotongan=0;
		$jmlKoperasi=0;
		foreach ($list as $key => $value) {
			// ambil status parameter dari grub kode bayar
			$this->db->where('id_grub_bayar_pk', $value['id_grub_kode_bayar_fk']);
			$gb = $this->db->get('hrd_grub_kode_bayar')->row_array();

			if ($gb['status_parameter'] == 1) {
				$jmlPendapatan = $jmlPendapatan + $value['nilai'];
				$dg[] = array('nama_kode_bayar' => $value['nama_kode_bayar'], 
							  'potongan' => 0,
							  'pendapatan' => $value['nilai'],
							  'kontribusi_koperasi' => 0
							  );
			} elseif ($gb['status_parameter'] == 2) {
				$jmlPotongan = $jmlPotongan + $value['nilai'];
				$dg[] = array('nama_kode_bayar' => $value['nama_kode_bayar'], 
							  'potongan' => $value['nilai'],
							  'pendapatan' => 0,
							  'kontribusi_koperasi' => 0
							  );
			} elseif ($gb['status_parameter'] == 3) {
				$jmlKoperasi = $jmlKoperasi + $value['nilai'];
				$dg[] = array('nama_kode_bayar' => $value['nama_kode_bayar'], 
							  'potongan' => 0,
							  'pendapatan' => 0,
							  'kontribusi_koperasi' => $value['nilai']
							  );
			}
		}

		$footer[] = array('nama_kode_bayar' => '<b>Total</b>', 'pendapatan' => $jmlPendapatan, 'potongan' => $jmlPotongan, 'kontribusi_koperasi' => $jmlKoperasi);
		$terima = $jmlPendapatan - $jmlPotongan;
		$TotalPengeluaranKop = $jmlPendapatan + $jmlKoperasi;
		$footer[] = array('nama_kode_bayar' => '<b>Total Pengeluaran Koperasi</b> (A+C)', 'pendapatan' => $TotalPengeluaranKop, 'potongan' => 0, 'kontribusi_koperasi' => 0);
		$footer[] = array('nama_kode_bayar' => '<b>Yang Diterima</b> (A-B)', 'pendapatan' => $terima, 'potongan' => 0, 'kontribusi_koperasi' => 0);

		$data = array('total' => count($dg), 'rows' => $dg, 'footer' => $footer);
		return json_encode( $data );
	}*/
}